package javaes.dados.generico;
import java.util.List;

import javaes.util.HibernateUtil;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

public class ObterRegistros<T>
{
	  T objeto;
	    private Session session; 
	    private Transaction transacao;
	    
	    public ObterRegistros(T objeto)
	    {
	        this.objeto = objeto;
	    }
	     /* Como usar
	      * ObterRegistros<Usuario> ob = new  ObterRegistros(new Usuario());
	        try {
	            List lista = ob.listar();
	            Iterator it = lista.iterator();
	            
	            while(it.hasNext())
	            {
	                Usuario us = (Usuario) it.next();
	                System.out.println(us.getNome());
	            }*/
	     @SuppressWarnings("unchecked")
		public List<T> listar() throws Exception
	    {
	        List<T> objGen                                          = null;
	        try
	        {
	            session                                             = HibernateUtil.getSessionFactory().getCurrentSession();
	            transacao                                           = session.beginTransaction();
	            Criteria filtro                                     = session.createCriteria(objeto.getClass());
	            objGen                                              = filtro.list();
	            transacao.commit();
	        }
	        catch(Exception erro)
	        {
	           if(transacao.isActive())
	               transacao.rollback();
	           throw new Exception(erro.getMessage());
	        }
	        finally
	        {
	            try
	            {
	            }
	            catch(Exception erro)
	            {
	                if(session.isOpen())
	                    session.close();
	                throw new Exception(erro.getMessage());
	            }
	        }
	        return objGen;
	    }
	     
   
	    public Object buscar(String filtro,String campo) throws Exception
	    {
	    	Object obj = null;
	    	try
	    	{
	    		 session = HibernateUtil.getSessionFactory().getCurrentSession();
	             transacao = this.session.beginTransaction();
	             Criteria busca =this.session.createCriteria(this.objeto.getClass());
	             busca.add(Restrictions.eq(campo,filtro));
	             obj = busca.uniqueResult();
	             this.transacao.commit();
	    	}
	    	catch(Exception erro)
	        {
	           if(transacao.isActive())
	               transacao.rollback();
	           throw new Exception(erro.getMessage());
	        }
	        finally
	        {
	            try
	            {
	            }
	            catch(Exception erro)
	            {
	                if(session.isOpen())
	                    session.close();
	                throw new Exception(erro.getMessage());
	            }
	        }
	    	return obj;
	    }
	    
	    public Object buscar(long filtro,String campo) throws Exception
	    {
	    	Object obj = null;
	    	try
	    	{
	    		 session = HibernateUtil.getSessionFactory().getCurrentSession();
	             transacao = this.session.beginTransaction();
	             Criteria busca =this.session.createCriteria(this.objeto.getClass());
	             busca.add(Restrictions.eq(campo,filtro));
	             obj = busca.uniqueResult();
	             this.transacao.commit();
	    	}
	    	catch(Exception erro)
	        {
	           if(transacao.isActive())
	               transacao.rollback();
	           throw new Exception(erro.getMessage());
	        }
	        finally
	        {
	            try
	            {
	            }
	            catch(Exception erro)
	            {
	                if(session.isOpen())
	                    session.close();
	                throw new Exception(erro.getMessage());
	            }
	        }
	    	return obj;
	    }
            
            	public List<T> listar(String campo,Object valor) throws Exception
	    {
	        List<T> objGen                                          = null;
	        try
	        {
	            session                                             = HibernateUtil.getSessionFactory().getCurrentSession();
	            transacao                                           = session.beginTransaction();
	            Criterion filtro                                     = Restrictions.eq(campo, valor);
                    Criteria pesquisa                                    = session.createCriteria(objeto.getClass());
                    pesquisa.add(filtro);
	            objGen                                              = pesquisa.list();
	            transacao.commit();
	        }
	        catch(Exception erro)
	        {
	           if(transacao.isActive())
	               transacao.rollback();
	           throw new Exception(erro.getMessage());
	        }
	        finally
	        {
	            try
	            {
	            }
	            catch(Exception erro)
	            {
	                if(session.isOpen())
	                    session.close();
	                throw new Exception(erro.getMessage());
	            }
	        }
	        return objGen;
	    }
}
